package com.webserver.core;
//ctrl+Alt+O  智能导包
import com.webserver.http.HttpServletRequest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * 实现多线程
 * 负责与连接的客户端进行HTTP交互
 * HTTP协议要求客户端与服务端采取一问一答的交互规则,并在交互完毕后断开TCP连接
 *
 * 因此,这里处理一次HTTP交互采取的过程分为三部分:
 * 1:解析请求(读取客户端发送过来的HTTP请求内容)
 * 2:处理请求(分析请求信息确定客户端意图,并进行对应的处理)
 * 3:发送响应(给浏览器发送一个HTTP中的响应,将处理结果回复给浏览器)
 */
public class ClientHandler implements Runnable{
    private Socket socket;
    public ClientHandler(Socket socket){
        this.socket = socket;
    }
    public void run() {
        try {
            // http://localhost:8088
            //1 解析请求 (实例化请求)
            //首先改为实例化HttpServletRequest完成解析请求(解析代码挪到HttpServletRequest的构造方法里)
            HttpServletRequest request=new HttpServletRequest(socket);

            //2 处理请求
            String path = request.getUri();
            System.out.println("请求的抽象路径"+path);
            /*
                浏览器地址栏输入路径   http://localhost:8088+抽象路径
                path = 抽象路径
                测试网址 http://localhost:8088    无法处理:不能被访问,不能访问目录
                测试网址 http://localhost:8088/myweb/123.html  无法处理:找不到指定文件
                http://localhost:8088/myweb/classTable.html
                http://localhost:8088/myweb/index.html
             */
            //File file =new File("./webapps/myweb/classTable.html");
            File file =new File("./webapps"+path);
            int statusCode;
            String statusReason;
            if (file.isFile()){
                statusCode = 200;
                statusReason = "OK";
            }
            else{
                statusCode = 404;
                statusReason = "Not Found";
                file = new File("./webapps/root/404.html");
            }
                //3 发送响应
            /*
                HTTP/1.1 200 OK(CRLF)
                Content-Type: text/html(CRLF)
                Content-Length: 2546(CRLF)(CRLF)
                10111010101010111......
             */
            //3.1发送状态行
            String line = "HTTP/1.1"+statusCode+" "+statusReason;
            println(line);
            //3.2发送响应头
            println("Content-Type: text/html");
            println("Content-Length" + file.length());
            println("");//单独发送回车换行
            //3.3发送响应正文
            FileInputStream fis = new FileInputStream(file);
            OutputStream out = socket.getOutputStream();
            byte[] data = new byte[1024 * 10];
            int len;
            while ((len = fis.read(data)) != -1) {
                out.write(data, 0, len);
            }
            System.out.println("响应发送完毕!");



        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //交互后与客户端断开连接(协议要求)
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private void println(String line) throws IOException{
        //通过socket获取输出流,用于给浏览器发送数据
        OutputStream out=socket.getOutputStream();
        //将要发送的字符串使用ISO_8859_1编码装换为一组字节
        byte[] data=line.getBytes(StandardCharsets.ISO_8859_1);
        //将这组字节发送给浏览器
        out.write(data);
        out.write(13);//单独发送回车
        out.write(10);//单独换行
    }

}
